package org.springframework.core.io.support;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Enumeration;
import java.util.Properties;

import org.springframework.util.ClassUtils;

public abstract class PropertiesLoaderUtils {

    public static Properties loadAllProperties(String resourceName,
            ClassLoader classLoader) throws IOException {
        ClassLoader clToUse = classLoader;
        if (clToUse == null) {
            clToUse = ClassUtils.getDefaultClassLoader();
        }
        Properties properties = new Properties();
        Enumeration urls = clToUse.getResources(resourceName);
        while (urls.hasMoreElements()) {
            URL url = (URL) urls.nextElement();
            InputStream is = null;
            try {
            URLConnection con = url.openConnection();
            con.setUseCaches(false);
            is = con.getInputStream();
            properties.load(is);
            } finally {
                is.close();
            }
        }
        return properties;
    }
}
